﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BOSS.Framework.Search.Context;

namespace BOSS.Framework.Search.Result
{
    /// <summary>
    /// 搜索结果
    /// </summary>
    public class SearchResult : ISearchResult
    {
        private IList<IResultItem> m_items;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"></param>
        public SearchResult(ISearchContext context)
            : this(context, 0, context.PageSize, context.PageIndex, string.Empty)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"></param>
        /// <param name="errorMessage"></param>
        public SearchResult(ISearchContext context, string errorMessage)
            : this(context, 0, 0, 1, errorMessage)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"></param>
        /// <param name="totalCount"></param>
        /// <param name="errorMessage"></param>
        public SearchResult(ISearchContext context, int totalCount, string errorMessage)
            : this(context, totalCount, context.PageSize, context.PageIndex, errorMessage)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"></param>
        /// <param name="totalCount"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="errorMessage"></param>
        public SearchResult(ISearchContext context, int totalCount, int pageSize, int pageIndex, string errorMessage)
        {
            this.SearchContext = context;
            this.TotalCount = totalCount;
            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            this.PageCount = 0;
            this.ErrorMessage = errorMessage;

            this.m_items = new List<IResultItem>();
        }

        #region ISearchResult 成员

        /// <summary>
        /// 获取 搜索上下文
        /// </summary>
        public ISearchContext SearchContext { get; private set; }

        /// <summary>
        /// 获取 内容类型
        /// </summary>
        public string ContentType { get { return this.SearchContext.ContentType; } }

        /// <summary>
        /// 获取 结果总数
        /// </summary>
        public int TotalCount { get; private set; }

        /// <summary>
        /// 获取 分页大小
        /// </summary>
        public int PageSize { get; private set; }

        /// <summary>
        /// 获取 分页的页序号，下标从 1 开始
        /// </summary>
        public int PageIndex { get; private set; }

        /// <summary>
        /// 获取 当前页的条目数
        /// </summary>
        public int PageCount { get; private set; }

        /// <summary>
        /// 获取 当前页的结果条目集合
        /// </summary>
        public IEnumerable<IResultItem> Items { get { return this.m_items; } }

        /// <summary>
        /// 获取 当前页的底层结构条目集合
        /// </summary>
        public IEnumerable<object> UnderlayerItems
        {
            get
            {
                return this.m_items.Select(delegate(IResultItem item)
                {
                    return item.UnderlayerObject;
                });
            }
        }

        /// <summary>
        /// 获取 错误信息
        /// </summary>
        public string ErrorMessage { get; private set; }

        /// <summary>
        /// 过滤结果条目
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ISearchResult Filter(Func<object, bool> filter)
        {
            SearchResult result = new SearchResult(this.SearchContext);
            foreach (var item in this.Items)
            {
                if (filter(item.UnderlayerObject))
                {
                    result.AddItem(item);
                }
            }
            return result;
        }

        /// <summary>
        /// 过滤结果条目
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ISearchResult Filter(Func<IResultItem, bool> filter)
        {
            SearchResult result = new SearchResult(this.SearchContext);
            foreach (var item in this.Items)
            {
                if (filter(item))
                {
                    result.AddItem(item);
                }
            }
            return result;
        }

        #endregion

        /// <summary>
        /// 添加当前页的结果条目
        /// </summary>
        /// <param name="item"></param>
        public void AddItem(IResultItem item)
        {
            this.m_items.Add(item);
            this.PageCount++;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
